﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LyncServerExtension.Monitoring
{
    /// <summary>
    /// Class to represent a file based event log.
    /// </summary>
    public class FileEventLog : IEventLog 
    {
        private static readonly object locker = new object();
        private string path;

        /// <summary>
        /// Initializes a new instance of the <see cref="FileEventLog"/> class.
        /// </summary>
        /// <param name="directoryPath">The log directory path.</param>
        public FileEventLog(string directoryPath)
        {
            if (directoryPath == null)
                throw new ArgumentNullException("directoryPath");

            if (!Directory.Exists(directoryPath))
                Directory.CreateDirectory(directoryPath);

            this.path = Path.Combine(directoryPath, string.Format("{0:ddMMyy-HHmmss}.log.txt", DateTime.Now));
        }

        /// <summary>
        /// Logs the specified event.
        /// </summary>
        /// <param name="eventType">Type of the event.</param>
        /// <param name="format">The format for the message.</param>
        /// <param name="args">The format arguments.</param>
        public void Log(EventType eventType, string format, params object[] args)
        {
            string message = string.Format("{0} [{1}] - {2}\r\n",
                    DateTime.Now.ToString(), eventType, string.Format(format, args));

            lock (locker)
            {
                File.AppendAllText(path, message);
            }
        }
    }
}
